//------------------------------------------------------------------------------
#include <QtGui>
#include <QHash>
#include <QHashIterator>
#include "CGrilleWidget.h"
//------------------------------------------------------------------------------
CGrilleWidget::CGrilleWidget(QList<CColumnModel *> *columnsModel, QWidget *parent, bool toolBarEnabled, bool canSwap, bool canFilter)
		: QWidget(parent)
{
	setupUi(this);
	onSwap=false;
	table->setWordWrap(true);
	table->setContextMenuPolicy(Qt::CustomContextMenu);
	
	connect(leFilterValue, SIGNAL(returnPressed()), this, SLOT(onFilter()));
	connect(toolButtonFilter, SIGNAL(clicked(bool)), this, SLOT(onFilter()));
	
	_toolBarEnabled=toolBarEnabled;
	_canSwap=canSwap;
	_canFilter=canFilter;
	
	filterWidget->setVisible(canFilter);
	
	toolButtonAjoute->setEnabled(toolBarEnabled); 
	toolButtonSupprime->setEnabled(toolBarEnabled); 
	toolButtonMonte->setEnabled(canSwap); 
	toolButtonDescend->setEnabled(canSwap); 
	
	setColumnsModel(columnsModel);
}
//------------------------------------------------------------------------------
void CGrilleWidget::setRowCount(int rows)
{
	table->setRowCount(rows);
	emit(firstRowChange(rows == 0));
}
//------------------------------------------------------------------------------
void CGrilleWidget::setItem(int row, int column, QTableWidgetItem * item)
{
	table->setItem(row, column, item);
}
//------------------------------------------------------------------------------
void CGrilleWidget::resizeRowsToContents(void)
{
	table->resizeRowsToContents();
}
//------------------------------------------------------------------------------
void CGrilleWidget::setColumnWidth(int column, int width)
{
	table->setColumnWidth(column, width);
}
//------------------------------------------------------------------------------
void CGrilleWidget::enableCommand(bool enable)
{
	toolButtonAjoute->setEnabled(_toolBarEnabled & enable); 
	toolButtonSupprime->setEnabled(_toolBarEnabled & enable); 
}
//------------------------------------------------------------------------------
void CGrilleWidget::enableSwap(bool swap)
{
	toolButtonMonte->setEnabled(_canSwap & swap); 
	toolButtonDescend->setEnabled(_canSwap & swap); 
}
//------------------------------------------------------------------------------
void CGrilleWidget::showFilter(bool show)
{
	filterWidget->setVisible(show);
}
//------------------------------------------------------------------------------
void CGrilleWidget::swapRows(int r1, int r2, bool *tableSwap)
{
	onSwap=true;
	if(r1 >=0 && r1 < table->rowCount() && r2 >= 0 && r2 < table->rowCount())
	{
		int i;
		
		for(i=0;i<table->columnCount();i++)
		{
			if(tableSwap[i])
			{
				QTableWidgetItem *t=table->takeItem(r1, i);
				table->setItem(r1, i, table->takeItem(r2, i));
				table->setItem(r2, i, t);
			}
		}
	}
	table->setCurrentCell(r2, table->currentColumn());
	onSwap=false;
	
	if(r1 == 0 || r2 == 0)
	{
		emit(firstRowChange(false));
	}
}
//------------------------------------------------------------------------------
void CGrilleWidget::setFilters(QHash<QString, QString> filters)
{
	QHashIterator<QString, QString> i(filters);
	
	while(i.hasNext())
	{
		i.next();
		cbFilterField->addItem(i.key(), i.value());
	}
}
//------------------------------------------------------------------------------
void CGrilleWidget::clearTable(void)
{
	int X, Y;
	for(Y=0;Y<table->rowCount();Y++)
	{
		for(X=0;X<table->columnCount();X++)
		{
			delete table->item(Y, X);
		}
	}
	table->setRowCount(0);
}
//------------------------------------------------------------------------------
int CGrilleWidget::loadGrille(QSqlQuery *query, QString primaryKeyName) {
	return table->loadGrille(query, primaryKeyName);
}
//------------------------------------------------------------------------------
QString CGrilleWidget::getFilter(void)
{
	QString text=leFilterValue->text().trimmed();
	QString filtre="";
	if(text != "")
	{
		filtre=cbFilterField->itemData(cbFilterField->currentIndex()).toString().arg(text);
	}
	
	return filtre;
}
//------------------------------------------------------------------------------
void CGrilleWidget::setColumnsModel(QList<CColumnModel *> *columnsModel)
{
	table->setColumnsModel(columnsModel);
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_toolButtonAjoute_clicked(bool checked)
{
	bool goToLast=true;
	
	emit(ajoute(goToLast));
	if(table->rowCount() == 1)
	{
		emit(firstRowChange(false));
	}
	
	if(goToLast)
	{
		table->verticalScrollBar()->setValue(table->verticalScrollBar()->maximum());
	}
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_toolButtonSupprime_clicked(bool checked)
{
	bool ok=false;
	int r=table->currentRow();
	emit(supprime(r, ok));
	
	if(ok)
	{
		table->removeRow(r);
		if(r == 0)
		{
			emit(firstRowChange(table->rowCount() == 0));
		}
	}
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_toolButtonExport_clicked(bool checked)
{
	table->exportCSV();
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_table_cellChanged(int row, int column)
{
	if(!onSwap) {
		emit(cellChanged(row, column));
		if(row == 0)
		{
			emit(firstRowChange(false));
		}
	}
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_table_cellDoubleClicked(int row, int column)
{
	emit(cellDoubleClicked(row, column));
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_table_customContextMenuRequested(const QPoint & pos)
{
	QPoint p=table->pos()+pos;
	
	p.rx()+=table->verticalHeader()->width();
	p.ry()+=table->horizontalHeader()->height();
	
	emit(customContextMenuRequested(p));
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_toolButtonMonte_clicked(bool checked)
{
	emit(monte(table->currentRow()));
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_toolButtonDescend_clicked(bool checked)
{
	emit(descend(table->currentRow()));
}
//------------------------------------------------------------------------------
void CGrilleWidget::onFilter(void)
{	
	emit(filter(getFilter()));
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_table_beforeInsert(const QSqlQuery *query, CMyTableWidgetItem *item)
{
	emit(beforeInsert(query, item));
}
//------------------------------------------------------------------------------
void CGrilleWidget::on_table_beforeChangeRecord(const QSqlQuery *query, int row)
{
	emit(beforeChangeRecord(query, row));
}
//------------------------------------------------------------------------------